package com.jlcloud.core.log.utils;

import org.slf4j.MDC;
import com.jlcloud.core.tool.utils.StringUtil;

/**
 * 日志追踪工具类
 *
 * @author By: JlCloud <br>
 * @Package: com.jlcloud <br>
 * @CreateTime: 2024-12-03 21:45 <br>
 * @Copyright: 2021 www.jilianjituan.com Inc. All rights reserved. <br>
 * @Caution 注意：本内容仅限于冀联人力集团内部传阅，禁止外泄以及用于其他的商业目的 <br>
 */
public class LogTraceUtil {
	public static final String UNIQUE_ID = "traceId";

	/**
	 * 获取日志追踪id格式
	 */
	public static String getTraceId() {
		return StringUtil.randomUUID();
	}

	public static String getTraceIdFromMDC() {
		return MDC.get(UNIQUE_ID);
	}

	/**
	 * 插入traceId
	 */
	public static boolean insert() {
		MDC.put(UNIQUE_ID, getTraceId());
		return true;
	}
	public static boolean insert(String traceId) {
		if (StringUtil.isBlank(traceId)) {
			traceId = getTraceId();
		}
		MDC.put(UNIQUE_ID, traceId);
		return true;
	}
	/**
	 * 移除traceId
	 */
	public static boolean remove() {
		MDC.remove(UNIQUE_ID);
		return true;
	}

}
